﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ZooSystem
{
    /// <summary>
    /// Логика взаимодействия для WorkerMain.xaml
    /// </summary>
    public partial class WorkerMain : Window
    {
        Serialization database;
        Animal current;
        String add = "<<Add animal>>";
        public WorkerMain()
        {
            InitializeComponent();
            database = Serialization.getDatabase();

            getAnimals();
            

            AnnimalType_CmbBox.Items.Add(AnimalType.Amphibian);
            AnnimalType_CmbBox.Items.Add(AnimalType.Arachnida);
            AnnimalType_CmbBox.Items.Add(AnimalType.Bird);
            AnnimalType_CmbBox.Items.Add(AnimalType.Chondrichthyes);
            AnnimalType_CmbBox.Items.Add(AnimalType.Mammal);
            AnnimalType_CmbBox.Items.Add(AnimalType.Primate);
            AnnimalType_CmbBox.Items.Add(AnimalType.Reptile);

        }

        private void getAnimals()
        {
            Animal_CmbBox.Items.Clear();
            foreach (Animal item in database.AnimalsList)
            {
                Animal_CmbBox.Items.Add(item);
            }
            Animal_CmbBox.Items.Add(add);
        }

        private void Exit_Btn_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Animal_CmbBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Animal_CmbBox.SelectedItem == null)
            {
                Properties_Grp.IsEnabled = false;
                Save_Btn.IsEnabled = false;
                Discard_Btn.IsEnabled = false;
            }
            else if (Animal_CmbBox.SelectedItem.ToString().Equals(add))
            {
                Properties_Grp.IsEnabled = true;
                Save_Btn.IsEnabled = true;
                Discard_Btn.IsEnabled = true;
                refreshData();
            }
            else
            {
                Properties_Grp.IsEnabled = true;
                Save_Btn.IsEnabled = true;
                Discard_Btn.IsEnabled = true;

                refreshData();

            }

        }

        public void refreshData()
        {
            if (!Animal_CmbBox.SelectedItem.ToString().Equals(add))
            {
                current = (Animal)Animal_CmbBox.SelectedItem;
                AnnimalType_CmbBox.SelectedItem = current.type;
                current.ChangeLocation();
            }
            else
            {
                current = new Animal();
                AnnimalType_CmbBox.SelectedIndex = -1;
            }

            Kind.Text = current.kind;
            Size.Text = current.size;
            Food.Text = current.foodPreferences;
            Activity.Text = current.activity;
            Place.Text = current.placeOfLive;
            Color.Text = current.color;
            SC_Name.Text = current.scientificName;
            Feature.Text = current.feature;
            Movement.Text = current.movement;
            Sound.Text = current.sound;
            Location.Text = current.coordinateX + "\n" + current.coordinateY;
            

            Hibernate_Chk.IsChecked = current.hibernate;
            Venomous_Chk.IsChecked = current.venomous;
        }

        private void Save_Btn_Click(object sender, RoutedEventArgs e)
        {
            Animal temp = new Animal();
            if (!Animal_CmbBox.SelectedItem.ToString().Equals(add))
                foreach (Animal a in database.AnimalsList)
                    if (a.ToString() == Animal_CmbBox.SelectedItem.ToString())
                    {
                        temp=a;
                        break;
                    }

            
            temp.type = (AnimalType)AnnimalType_CmbBox.SelectedItem;
            temp.kind = Kind.Text;
            temp.size = Size.Text;
            temp.foodPreferences = Food.Text;
            temp.activity = Activity.Text;
            temp.placeOfLive = Place.Text;
            temp.color = Color.Text;
            temp.scientificName = SC_Name.Text;
            temp.feature = Feature.Text;
            temp.movement = Movement.Text;
            temp.sound = Sound.Text;

            temp.hibernate = (bool)Hibernate_Chk.IsChecked;
            temp.venomous = (bool)Venomous_Chk.IsChecked;

            if (Animal_CmbBox.SelectedItem.ToString().Equals(add))
                database.AddAnimal(temp);

            int SelectedAnimal = Animal_CmbBox.SelectedIndex;
            getAnimals();
            Animal_CmbBox.SelectedIndex = SelectedAnimal;
        }

        private void Discard_Btn_Click(object sender, RoutedEventArgs e)
        {
            refreshData();
        }
    }
}
